home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / counter.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  7KB  |  230 lines

  1. /*
  2.  * counter.c
  3.  *
  4.  * Forms Object class: COUNTER
  5.  *
  6.  * Written by: Mark Overmars
  7.  *
  8.  * Version 2.1 a
  9.  * Date: Sep 29, 1992
  10.  */
  11.  
  12. #include <malloc.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "forms.h"
  17.  
  18. /* The special information for counters. */
  19. typedef struct {
  20.    float value;        /* Value of the slider */
  21.    float min,max;    /* Bounds */
  22.    float sstep,lstep;    /* Step sizes */
  23.    int prec;            /* precision when displaying value */
  24.    int always;          /* whether always returning value */
  25.    int mouseobj;    /* Button under the mouse */
  26. } SPEC;
  27.  
  28. static int timdel;        /* Delay since last value change */
  29.  
  30. static void draw_counter(FL_OBJECT *ob)
  31. /* Draws a counter */
  32. {
  33.   float xx[5], ww[5];
  34.   char str[64];
  35.   int i, btype[5];
  36.   SPEC *sp = (SPEC *) ob->spec;
  37.   /* Compute boxtypes */
  38.   for (i=0; i<5; i++)
  39.     if (sp->mouseobj == i && ob->boxtype == FL_UP_BOX)
  40.       btype[i] = FL_DOWN_BOX;
  41.     else
  42.       btype[i] = ob->boxtype;
  43.   if (btype[4] == FL_UP_BOX) btype[4] = FL_DOWN_BOX;
  44.   /* Compute sizes */
  45.   if (ob->type == FL_NORMAL_COUNTER)
  46.   {
  47.     xx[0] = ob->x + 0.00 * ob->w; ww[0] = 0.15 * ob->w;
  48.     xx[1] = ob->x + 0.15 * ob->w; ww[1] = 0.15 * ob->w;
  49.     xx[2] = ob->x + 0.70 * ob->w; ww[2] = 0.15 * ob->w;
  50.     xx[3] = ob->x + 0.85 * ob->w; ww[3] = 0.15 * ob->w;
  51.     xx[4] = ob->x + 0.30 * ob->w; ww[4] = 0.40 * ob->w;
  52.   }
  53.   else
  54.   {
  55.     xx[1] = ob->x + 0.00 * ob->w; ww[1] = 0.20 * ob->w;
  56.     xx[2] = ob->x + 0.80 * ob->w; ww[2] = 0.20 * ob->w;
  57.     xx[4] = ob->x + 0.20 * ob->w; ww[4] = 0.60 * ob->w;
  58.   }
  59.   /* Create value string */
  60.   /* Draw the thing */
  61.   sprintf(str,"%.*f",sp->prec,sp->value);
  62.   if (ob->type == FL_NORMAL_COUNTER)
  63.   {
  64.    fl_drw_box(btype[0],xx[0],ob->y,ww[0],ob->h,ob->col1,FL_COUNTER_BW);
  65.    fl_drw_text(FL_ALIGN_CENTER,xx[0],ob->y,ww[0],ob->h,ob->col2,0.0,0,"@<<");
  66.   }
  67.   fl_drw_box(btype[1],xx[1],ob->y,ww[1],ob->h,ob->col1,FL_COUNTER_BW);
  68.   fl_drw_text(FL_ALIGN_CENTER,xx[1],ob->y,ww[1],ob->h,ob->col2,0.0,0,"@<");
  69.   fl_drw_box(btype[4],xx[4],ob->y,ww[4],ob->h,ob->col1,FL_COUNTER_BW);
  70.   fl_drw_text(FL_ALIGN_CENTER,xx[4],ob->y,ww[4],ob->h,
  71.             ob->lcol,ob->lsize,ob->lstyle,str);
  72.   fl_drw_box(btype[2],xx[2],ob->y,ww[2],ob->h,ob->col1,FL_COUNTER_BW);
  73.   fl_drw_text(FL_ALIGN_CENTER,xx[2],ob->y,ww[2],ob->h,ob->col2,0.0,0,"@>");
  74.   if (ob->type == FL_NORMAL_COUNTER)
  75.   {
  76.    fl_drw_box(btype[3],xx[3],ob->y,ww[3],ob->h,ob->col1,FL_COUNTER_BW);
  77.    fl_drw_text(FL_ALIGN_CENTER,xx[3],ob->y,ww[3],ob->h,ob->col2,0.0,0,"@>>");
  78.   }
  79.   fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,
  80.                         ob->lcol,ob->lsize,ob->lstyle,ob->label);
  81. }
  82.  
  83. static void calc_mouse_obj(FL_OBJECT * ob, float mx, float my)
  84. {
  85.   SPEC *sp = (SPEC *) ob->spec;
  86.   sp->mouseobj = -1;
  87.   if (my<ob->y || my>ob->y + ob->h) return;
  88.   if (ob->type == FL_NORMAL_COUNTER)
  89.   {
  90.     if (mx < ob->x + 0.15 * ob->w) sp->mouseobj = 0;
  91.     else if (mx < ob->x + 0.30 * ob->w) sp->mouseobj = 1;
  92.     else if (mx < ob->x + 0.70 * ob->w) sp->mouseobj = 4;
  93.     else if (mx < ob->x + 0.85 * ob->w) sp->mouseobj = 2;
  94.     else if (mx < ob->x + 1.00 * ob->w) sp->mouseobj = 3;
  95.   }
  96.   else
  97.   {
  98.     if (mx < ob->x + 0.20 * ob->w) sp->mouseobj = 1;
  99.     else if (mx < ob->x + 0.80 * ob->w) sp->mouseobj = 4;
  100.     else if (mx < ob->x + 1.00 * ob->w) sp->mouseobj = 2;
  101.   }
  102. }
  103.  
  104. static int handle_mouse(FL_OBJECT *ob, int event, float mx, float my)
  105. /* handles an event on ob*/
  106. {
  107.   SPEC *sp = (SPEC *) ob->spec;
  108.   int changeval = 0;
  109.   if (event == FL_RELEASE)
  110.     { sp->mouseobj = -1; fl_redraw_object(ob); return 0;}
  111.   else if (event == FL_PUSH)
  112.     { calc_mouse_obj(ob,mx,my); if (sp->mouseobj != -1) changeval = 1; timdel = 1;}
  113.   else if (event == FL_MOUSE && sp->mouseobj != -1)
  114.     changeval = (timdel++ % 12 == 0);
  115.   if (changeval)
  116.   {
  117.     if (sp->mouseobj == 0) sp->value -= sp->lstep;
  118.     if (sp->mouseobj == 1) sp->value -= sp->sstep;
  119.     if (sp->mouseobj == 2) sp->value += sp->sstep;
  120.     if (sp->mouseobj == 3) sp->value += sp->lstep;
  121.     if (sp->value < sp->min) sp->value = sp->min;
  122.     if (sp->value > sp->max) sp->value = sp->max;
  123.     fl_redraw_object(ob);
  124.   }
  125.   return changeval;
  126. }
  127.  
  128. static int valchanged;    /* whether value has changed since last time */
  129.  
  130. static int handle_counter(FL_OBJECT *ob,int event,float mx,float my,char key)
  131. /* Handles an event */
  132. {
  133.   SPEC *sp = (SPEC *) ob->spec;
  134.   float val;
  135.   switch (event)
  136.   {
  137.     case FL_DRAW:
  138.         draw_counter(ob);
  139.         return 0;
  140.     case FL_PUSH:
  141.     valchanged = 0;
  142.       if (handle_mouse(ob,event,mx,my))
  143.       { if (sp->always) return 1; else { valchanged = 1; return 0;} }
  144.         break;
  145.     case FL_MOUSE:
  146.       if (handle_mouse(ob,event,mx,my))
  147.       { if (sp->always) return 1; else { valchanged = 1; return 0;} }
  148.         break;
  149.     case FL_RELEASE:
  150.     return (handle_mouse(ob,event,mx,my) || (! sp->always && valchanged));
  151.     case FL_FREEMEM:
  152.     free(ob->spec);
  153.     return 0;
  154.   }
  155.   return 0;
  156. }
  157.  
  158. /*------------------------------*/
  159.  
  160. FL_OBJECT *fl_create_counter(int type,float x,float y,float w,float h,char label[])
  161. /* creates an object */
  162. {
  163.   FL_OBJECT *ob;
  164.   SPEC *sp;
  165.   ob = fl_make_object(FL_COUNTER,type,x,y,w,h,label,handle_counter);
  166.   ob->boxtype = FL_COUNTER_BOXTYPE;
  167.   ob->col1 = FL_COUNTER_COL1;
  168.   ob->col2 = FL_COUNTER_COL2;
  169.   ob->align = FL_COUNTER_ALIGN;
  170.   ob->lcol = FL_COUNTER_LCOL;
  171.  
  172.   ob->spec = (int *) fl_malloc(sizeof(SPEC));
  173.   sp = ((SPEC *)(ob->spec));
  174.   sp->min = -1000000.0;
  175.   sp->max = 1000000.0;
  176.   sp->sstep = 0.1;
  177.   sp->lstep = 1.0;
  178.   sp->value = 0.0;
  179.   sp->prec = 1;
  180.   sp->always = FALSE;
  181.   sp->mouseobj = -1;
  182.   
  183.   return ob;
  184. }
  185.  
  186. FL_OBJECT *fl_add_counter(int type, float x, float y, float w, float h, char label[])
  187. /* Adds an object */
  188. {
  189.   FL_OBJECT *ob;
  190.   ob = fl_create_counter(type,x,y,w,h,label);
  191.   fl_add_object(fl_current_form,ob);
  192.   return ob;
  193. }
  194.  
  195. void fl_set_counter_value(FL_OBJECT *ob, float val)
  196.   ((SPEC *)(ob->spec))->value = val;
  197.   fl_redraw_object(ob);
  198. }
  199.  
  200. void fl_set_counter_bounds(FL_OBJECT *ob, float min, float max)
  201.   ((SPEC *)(ob->spec))->min = min;
  202.   ((SPEC *)(ob->spec))->max = max;
  203.   fl_redraw_object(ob);
  204. }
  205.  
  206. void fl_set_counter_step(FL_OBJECT *ob, float small, float large)
  207.   ((SPEC *)(ob->spec))->sstep = small;
  208.   ((SPEC *)(ob->spec))->lstep = large;
  209.   fl_redraw_object(ob);
  210. }
  211.  
  212. void fl_set_counter_precision(FL_OBJECT *ob,int prec)
  213.   ((SPEC *)(ob->spec))->prec = prec;
  214.   fl_redraw_object(ob);
  215. }
  216.  
  217. float fl_get_counter_value(FL_OBJECT *ob)
  218.   { return ((SPEC *)(ob->spec))->value; }
  219.  
  220. void fl_set_counter_return(FL_OBJECT *ob, int value)
  221. /* Sets whether to return value all the time */
  222. {
  223.   ((SPEC *)(ob->spec))->always = value;
  224. }
  225.  
  226.